<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .login {
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            position: fixed;
            left: 0;
            top: 0;
            z-index: 2;
            background-color: #fff;
        }

        .login input {
            display: block;
            width: 200px;
            height: 50px;
            margin-bottom: 20px;
        }

        .login button {
            display: block;
            width: 200px;
            height: 50px;
        }

        #box {
            margin: 50px auto;
            width: 500px;
            height: 600px;
            border: 1px solid #000;
            position: relative;
        }

        .userList {
            position: absolute;
            top: -1px;
            right: -102px;
            height: 100%;
            width: 100px;
            border: 1px solid #000;
            overflow: hidden;
        }

        .list {
            width: 100%;
            height: 88%;
            padding: 30px;
            box-sizing: border-box;
            outline: 1px solid #000;
            outline-offset: -20px;
        }

        .list div span {
            margin-right: 20px;
        }

        .opt {
            height: 12%;
            display: flex;
            align-items: center;
            justify-content: space-around;
        }

        .opt div {
            width: 100px;
            height: 20px;
            background-color: #f00;
            color: #fff;
        }
    </style>
</head>
<body>
<div class="login">
    <input type="text" placeholder="请输入姓名" id="userName"/>
    <button onclick="login()">登录</button>
</div>
<div id="box">
    <div class="list"></div>
    <div class="opt">
        <input type="text" id="val" placeholder="请输入需要提交的内容"/>

        <div onclick="btn()">提交</div>
    </div>
    <div class="userList"></div>
</div>
<!--引库-->
<script src="http://localhost:8022/socket.io/socket.io.js"></script>
<script>
    //    连接socket:
    var socket = io.connect('ws://localhost:8022'),userName='';
    //    *.emit为发送事件，*.on为接收事件
    function login() {
        userName=document.querySelector("#userName").value;
        socket.emit("login", userName);
        document.querySelector(".login").style.display = "none";
    }
    function btn() {
        socket.emit("sendMsg",{name:userName,cont:document.querySelector("#val").value});
        document.querySelector("#val").value = "";
    }
    socket.on("userBack", function (res) {
        var arr = JSON.parse(res);
        document.querySelector(".userList").innerHTML = '';
        for (i in arr) {
            document.querySelector(".userList").innerHTML += '<p>' + arr[i] + '</p>';
        }
    });
    socket.on("sendMsgBack", function (res) {
        var list = JSON.parse(res);
        document.querySelector(".list").innerHTML = '';
        for (i in list) {
            document.querySelector(".list").innerHTML += '<div><span>'+list[i].name+'</span><span>'+list[i].time+'</span><p>'+list[i].cont+'</p></div>';
        }
    });
</script>
</body>
</html>